import 'package:enter_prise_web/util/MyDio.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class EditPwd extends StatefulWidget {
  const EditPwd({super.key, required this.uid});
  final String uid;

  @override
  State<EditPwd> createState() => _EditPwdState();
}

class _EditPwdState extends State<EditPwd> {
  final _oldPwdController = TextEditingController();
  final _newPwdController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 7, 152, 200),
      appBar: AppBar(
        title: const Text("修改密码"),
        centerTitle: true,
      ),
      body: // Generated code for this Column Widget...
          Center(
        child: Padding(
          padding: const EdgeInsets.all(24),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              Padding(
                padding: const EdgeInsetsDirectional.fromSTEB(20, 20, 20, 0),
                child: TextFormField(
                  controller: _oldPwdController,
                  // focusNode: _model.emailAddressFocusNode,
                  obscureText: false,
                  decoration: InputDecoration(
                    hintText: "原密码",
                    // labelStyle: FlutterFlowTheme.of(context).bodySmall,
                    enabledBorder: OutlineInputBorder(
                      borderSide: const BorderSide(
                        color: Colors.white,
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: const BorderSide(
                        color: Color(0x00000000),
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    errorBorder: OutlineInputBorder(
                      borderSide: const BorderSide(
                        color: Color(0x00000000),
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderSide: const BorderSide(
                        color: Color(0x00000000),
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    filled: true,
                    fillColor: Colors.white,
                    contentPadding:
                        const EdgeInsetsDirectional.fromSTEB(20, 16, 20, 16),
                  ),

                  // validator: _model.emailAddressControllerValidator
                  //     .asValidator(context),
                ),
              ),
              Padding(
                padding: const EdgeInsetsDirectional.fromSTEB(20, 12, 20, 0),
                child: TextFormField(
                  controller: _newPwdController,
                  // focusNode: _model.passwordFocusNode,
                  decoration: InputDecoration(
                    hintText: "新密码",
                    enabledBorder: OutlineInputBorder(
                      borderSide: const BorderSide(
                        color: Colors.white,
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: const BorderSide(
                        color: Color(0x00000000),
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    errorBorder: OutlineInputBorder(
                      borderSide: const BorderSide(
                        color: Color(0x00000000),
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    focusedErrorBorder: OutlineInputBorder(
                      borderSide: const BorderSide(
                        color: Color(0x00000000),
                        width: 1,
                      ),
                      borderRadius: BorderRadius.circular(8),
                    ),
                    filled: true,
                    fillColor: Colors.white,
                    contentPadding:
                        const EdgeInsetsDirectional.fromSTEB(20, 16, 20, 16),
                    suffixIcon: InkWell(
                      onTap: () {},
                      focusNode: FocusNode(skipTraversal: true),
                      child: const Icon(
                        Icons.visibility_off_outlined,
                        // color: FlutterFlowTheme.of(context).secondaryText,
                        size: 16,
                      ),
                    ),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsetsDirectional.fromSTEB(0, 24, 0, 0),
                child: Container(
                  width: 80,
                  height: 40,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(5),
                      color: Colors.white),
                  child: TextButton(
                    onPressed: () {
                      String old = _oldPwdController.text;
                      String new1 = _newPwdController.text;
                      _oldPwdController.clear();
                      _newPwdController.clear();

                      if (old == "" || new1 == "") {
                        Fluttertoast.showToast(msg: "密码不能为空");
                      } else if (old == new1) {
                        Fluttertoast.showToast(msg: "新密码不能与旧密码相同");
                      } else {
                        // 请求接口
                        MyDio myDio = MyDio();
                        var response =  myDio.requestData("/user/change",method: 'POST', params: {
                          "uid": widget.uid,
                          'oldPass': old,
                          'newPass': new1
                        });

                      

                        response.then((v) {
                          print("${v.data}ee");
                          print("${v}ee");
                          print("${v.statusCode}ee");

                          if (int.parse(v.data) == 2) {
                            Fluttertoast.showToast(msg: "原密码错误");
                          } else if (int.parse(v.data) == 1) {
                            Fluttertoast.showToast(msg: "修改成功");
                            Get.back(); // 返回上一页并传递修改成功的标志位true
                          } else {
                            Fluttertoast.showToast(msg: "未知错误");
                          }
                        });
                      }
                    },
                    style: TextButton.styleFrom(
                      elevation: 0,
                    ),
                    child: const Text(
                      "修改",
                      style: TextStyle(fontSize: 17),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
